package com.pets.web.controller.api;

import com.pets.common.annotation.RateLimiter;
import com.pets.common.core.controller.BaseController;
import com.pets.common.core.domain.R;
import com.pets.common.core.page.TableDataInfo;
import com.pets.common.enums.LimitType;
import com.pets.web.dto.CreateOrderDto;
import com.pets.web.entity.PetsOrders;
import com.pets.web.service.IPetOrdersService;
import com.pets.web.vo.ApiPetOrdersSearchRequest;
import com.pets.web.vo.CreateOrderRequest;
import com.pets.web.vo.PetOrdersSearchRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "订单相关接口")
@Validated
@RestController
@RequestMapping("/api/order")
public class ApiPetOrderController extends BaseController {

    @Autowired
    private IPetOrdersService petOrdersService;

    @ApiOperation("订单列表接口")
    @GetMapping("/list")
    public TableDataInfo<PetsOrders> list(@Validated ApiPetOrdersSearchRequest request){
        startPage();
        List<PetsOrders> list = petOrdersService.list(request);
        return getDataTable(list);
    }

    @ApiOperation("购买记录接口")
    @GetMapping("/recordList")
    public TableDataInfo<PetsOrders> recordList(Integer goodsId){
        startPage();
        List<PetsOrders> list = petOrdersService.recordList(goodsId);
        return getDataTable(list);
    }

    @ApiOperation("创建订单")
    @RateLimiter(time = 5, count = 5, limitType = LimitType.IP)
    @PostMapping("/create")
    public R<CreateOrderDto> create(@Validated @RequestBody CreateOrderRequest request) {
        CreateOrderDto dto = petOrdersService.create(request);
        return R.ok(dto);
    }

    /**
     * 微信支付回调
     */
    @ApiOperation("微信支付回调")
    @PostMapping("/notify")
    public ResponseEntity<Void> notify(HttpServletRequest request, @RequestBody String body) {
        return petOrdersService.notify(request, body);
    }
}
